package com.tool

import android.text.TextUtils
import java.security.MessageDigest

/**
 * Created by mac on 2017/8/11.
 */
class KSHA256 {

    companion object {
        fun sha256(plainText: String?): String? {
            if (TextUtils.isEmpty(plainText)) return ""
            val digest: MessageDigest
            try {
                digest = MessageDigest.getInstance("SHA-256")
                val hash = digest.digest(plainText?.toByteArray(charset("UTF-8")))
                val hexString = StringBuilder()
                for (hex in hash) {
                    val hexStr = Integer.toHexString(0xff and hex.toInt())
                    if (hexStr.length == 1) hexString.append('0')
                    hexString.append(hexStr)
                }
                return hexString.toString()
            } catch (e: Throwable) {
                e.printStackTrace()
            }
            return plainText
        }
    }

}